# 功能模块：车间场景
# 作者：dl
# 描述：车间场景

import os
import asyncio
from gltf import GLTFLoader, GLTFRenderEngine
import vtk
from visualizationCom.tools.color import hex_to_rgba

class workshopScene:
    # 构造函数
    def __init__(self, parent, webState, webCtrl):
        self.parent = parent
        self.webState = webState
        self.webCtrl = webCtrl
        # 车间场景相关属性
        self.hasLoadScene = False  # 用于判断是否存在缓存
        
        self.engine = GLTFRenderEngine()
        self.renderer = self.engine.GetDefaultRenderer()    
        self.camera = self.engine.GetCamera()

        # 临时解决renderer自带光源因为角度照不到的问题
        self.renderer.CreateLight()

        bg_color = hex_to_rgba('#132c4a')
        self.renderer.SetBackground(bg_color[0], bg_color[1], bg_color[2])

    def enterScene(self):

        self.parent.mouseInteractorEvent.outline.Clear()
        self.parent.mouseInteractorEvent.lastPick = None

        with self.webState:
            self.webState.sceneKey = 'workshop'

        self.parent.currentRenderer = self.renderer
        
        # 暂时写死模型
        if self.hasLoadScene == False:

            camera = self.camera
            self.initial_position = camera.GetPosition()
            self.initial_focal_point = camera.GetFocalPoint()
            self.initial_view_up = camera.GetViewUp()

            CURRENT_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

            modelPath = "model/workshop/productionline.gltf"

            # 结合 CURRENT_DIRECTORY 和 modelPath 形成新的地址
            new_directory = os.path.join(CURRENT_DIRECTORY, modelPath)
            self.load_file(new_directory)

        self.hasLoadScene = True

        # 获取当前相机
        self.renderer.SetActiveCamera(self.camera)

        self.parent.renderWindow.AddRenderer(self.renderer)

        # 重置交互器
        self.engine.SetRenderWindow(self.parent.renderWindow)
        self.parent.mouseInteractorEvent.SetDefaultRenderer(self.renderer)
        self.parent.mouseInteractorEvent.SetOutlineRenderer(self.engine.GetOutlineRenderer())

        self.restCamera()
        self.parent.renderWindow.Render()
        
        self.add3DText()

        self.webCtrl.view_update()
        self.webCtrl.sendMsgToParent({"eventScene":"workshop","eventName":"enterScene","value":"complete"})

    def leaveScene(self):
        self.parent.renderWindow.RemoveRenderer(self.renderer)

    def restCamera(self):
        # 获取当前相机
        camera = self.renderer.GetActiveCamera()

        # 重置相机的位置和方向为初始状态
        camera.SetPosition(self.initial_position)
        camera.SetFocalPoint(self.initial_focal_point)
        camera.SetViewUp(self.initial_view_up)

        camera.Elevation(20) #沿z轴旋转
        # camera.Azimuth(135) 沿Y轴旋转

        self.renderer.ResetCamera()

    def changeEvn(self,bgIndex):
        if bgIndex == 1:
            # CURRENT_DIRECTORY = os.path.dirname(
            #     os.path.dirname(os.path.dirname(__file__)))

            # modePath = "3dENV/spaichingen_hill_1k.hdr"

            # # 结合 CURRENT_DIRECTORY 和 modePath 形成新的地址
            # new_directory = os.path.join(CURRENT_DIRECTORY, modePath)
            # self.renderer.SetEnvmap(new_directory)
            bg_color = hex_to_rgba('#041A21')
            self.renderer.SetBackground(bg_color[0], bg_color[1], bg_color[2])
        else:
            bg_color = hex_to_rgba('#132c4a')
            self.renderer.SetBackground(bg_color[0], bg_color[1], bg_color[2])
        self.parent.renderWindow.Render()
        self.webCtrl.view_update()
        self.webCtrl.view_update()

    def load_file(self, file_name):
        self.renderer.RemoveAllViewProps()

        self.reader_detail = GLTFLoader()
        self.reader_detail.Load(file_name)
        self.renderer.AddScene(self.reader_detail.GetScene())

        self.reader_detail_two = GLTFLoader()
        self.reader_detail_two.Load(file_name)
        scene_two = self.reader_detail_two.GetScene()
        scene_two.transform.Translate(40, 0, 0)
        scene_two.transform.RotateWXYZ(90, 0, 1, 0)
        self.renderer.AddScene(scene_two)

        self.reader_detail_three = GLTFLoader()
        self.reader_detail_three.Load(file_name)
        scene_three = self.reader_detail_two.GetScene()
        scene_three.transform.Translate(-40, 0, 0)
        scene_three.transform.RotateWXYZ(90, 0, 1, 0)
        self.renderer.AddScene(scene_three)

    def add3DText(self):
        # 设置中文字体文件路径
        CURRENT_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))

        user_input = "font/"

        self.textActor = vtk.vtkTextActor3D()
        self.textActor.SetInput("产线1")  # 设置中文文字 换行格式 设备故障\n2023-12-26

        # 使用FreeType字体库和字符纹理映射
        textProperty = self.textActor.GetTextProperty()
        textProperty.SetFontFamily(vtk.VTK_FONT_FILE)  # 设置字体为FreeType字体库

        # 结合 CURRENT_DIRECTORY 和 user_input 形成新的地址
        new_directory = os.path.join(CURRENT_DIRECTORY, user_input)
        textProperty.SetFontFile(
            new_directory + "sanjizihaisonggbk.ttf")

        self.textActor.GetTextProperty().SetColor(1, 1, 1)  # 设置文字颜色为白色
        self.textActor.GetTextProperty().SetFontSize(18)  # 设置文字大小
        self.textActor.SetScale(0.2)
        self.textActor.SetPosition(1, 10, 0.01)

        # 文字2
        self.textActor_2 = vtk.vtkTextActor3D()
        self.textActor_2.SetInput("产线2")  # 设置中文文字 换行格式 设备故障\n2023-12-26

        # 使用FreeType字体库和字符纹理映射
        textProperty_2 = self.textActor_2.GetTextProperty()
        textProperty_2.SetFontFamily(vtk.VTK_FONT_FILE)  # 设置字体为FreeType字体库

        textProperty_2.SetFontFile(
            new_directory + "sanjizihaisonggbk.ttf")

        self.textActor_2.GetTextProperty().SetColor(1, 1, 1)  # 设置文字颜色为白色
        self.textActor_2.GetTextProperty().SetFontSize(18)  # 设置文字大小
        self.textActor_2.SetScale(0.2)
        self.textActor_2.SetPosition(-42, 20, 0.01)

        # 文字3
        self.textActor_3 = vtk.vtkTextActor3D()
        self.textActor_3.SetInput("产线3")  # 设置中文文字 换行格式 设备故障\n2023-12-26

        # 使用FreeType字体库和字符纹理映射
        textProperty_3 = self.textActor_3.GetTextProperty()
        textProperty_3.SetFontFamily(vtk.VTK_FONT_FILE)  # 设置字体为FreeType字体库

        textProperty_3.SetFontFile(
            new_directory + "sanjizihaisonggbk.ttf")

        self.textActor_3.GetTextProperty().SetColor(1, 1, 1)  # 设置文字颜色为白色
        self.textActor_3.GetTextProperty().SetFontSize(18)  # 设置文字大小
        self.textActor_3.SetScale(0.2)
        self.textActor_3.SetPosition(43, 20, 0.01)

        # 读入图像数据
        user_input = "images/"

        # 结合 CURRENT_DIRECTORY 和 user_input 形成新的地址
        new_directory = os.path.join(CURRENT_DIRECTORY, user_input)
        reader = vtk.vtkPNGReader()
        reader.SetFileName(new_directory + "dialogbox_new.png")
        reader.Update()

        # 创建图像显示对象
        self.imageActor_dialogbox = vtk.vtkImageActor()
        self.imageActor_dialogbox.SetInputData(reader.GetOutput())
        self.imageActor_dialogbox.SetPosition(-3, 3, 0)
        self.imageActor_dialogbox.SetScale(0.08)
        self.imageActor_dialogbox.SetOpacity(0.7)

        self.renderer.AddActor(self.imageActor_dialogbox)
        self.renderer.AddActor(self.textActor)

        # 创建图像显示对象2
        self.imageActor_dialogbox_2 = vtk.vtkImageActor()
        self.imageActor_dialogbox_2.SetInputData(reader.GetOutput())
        self.imageActor_dialogbox_2.SetPosition(-45, 13, 0)
        self.imageActor_dialogbox_2.SetScale(0.08)
        self.imageActor_dialogbox_2.SetOpacity(0.7)

        self.renderer.AddActor(self.imageActor_dialogbox_2)
        self.renderer.AddActor(self.textActor_2)

        # 创建图像显示对象3
        self.imageActor_dialogbox_3 = vtk.vtkImageActor()
        self.imageActor_dialogbox_3.SetInputData(reader.GetOutput())
        self.imageActor_dialogbox_3.SetPosition(39, 13, 0)
        self.imageActor_dialogbox_3.SetScale(0.08)
        self.imageActor_dialogbox_3.SetOpacity(0.7)

        self.renderer.AddActor(self.imageActor_dialogbox_3)
        self.renderer.AddActor(self.textActor_3)


